﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;


namespace application.control
{
    public class gridVoucher : common.controls.baseDataGridView 
    {
        public gridVoucher()
        {
            this.AutoGenerateColumns = false;
            this.myAutoFitColumn = ColumnNames.notes.ToString();
        }
        public delegate void ShowVoucher(string voucherId, string voucherType);
        public ShowVoucher myShowVoucher = null;

        public enum ColumnNames
        {
            voucherNo, notes, onDate, editColumn
        };

        public void Init(data.tmpDS.voucherBriefDataTable tbl)
        {
            this.DataSource = tbl;
            // 
            // voucherNo
            // 
            common.controls.DataGridViewTextBoxColumnExt voucherNo = new common.controls.DataGridViewTextBoxColumnExt();
            voucherNo.Uppercase = true;
            voucherNo.DataPropertyName = tbl.voucherNoColumn.ColumnName;
            voucherNo.HeaderText = "Số phiếu";
            voucherNo.Name = ColumnNames.voucherNo.ToString();
            voucherNo.Width = 160;

            // 
            // onDate
            // 
            DataGridViewTextBoxColumn onDate = new common.controls.DataGridViewTextBoxColumnExt();
            onDate.DataPropertyName = tbl.onDateColumn.ColumnName;
            onDate.HeaderText = "Ngày";
            onDate.Name = ColumnNames.onDate.ToString();
            onDate.Width = 90;

            DataGridViewCellStyle dateType = new DataGridViewCellStyle();
            dateType.Format = "d";
            dateType.NullValue = null;
            onDate.DefaultCellStyle = dateType;

            // 
            // description
            // 
            DataGridViewTextBoxColumn description = new common.controls.DataGridViewTextBoxColumnExt();
            description.DataPropertyName = tbl.notes1Column.ColumnName;
            description.HeaderText = "Diễn giải";
            description.Name = ColumnNames.notes.ToString();

            //editColumn
            common.controls.gridViewImageColumn editColumn = new common.controls.gridViewImageColumn();
            editColumn.Name = ColumnNames.editColumn.ToString(); 
            editColumn.myImageType = common.controls.imageType.Edit;
            editColumn.Width = 25;
            editColumn.HeaderText = "";

            // 
            // myDetailGrid
            // 
            this.Columns.Clear();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { voucherNo, onDate, description, editColumn });

            AutoArrange();
        }

        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            base.OnCellClick(e);
            if (myShowVoucher == null) return;
            if (e.ColumnIndex < 0 || e.ColumnIndex < 0) return;
            if (this.Columns[e.ColumnIndex].Name != ColumnNames.editColumn.ToString()) return;
            if (this.CurrentRow==null) return;
            data.tmpDS.voucherBriefRow row = (data.tmpDS.voucherBriefRow)(this.CurrentRow.DataBoundItem as DataRowView).Row;
            myShowVoucher(row.voucherId, row.voucherType); 
        }
    }
}
